<template>
  <div>
    <el-card>
      <!-- 添加按钮 -->
      <el-button type="primary">添加用户</el-button>
      <!-- 表格  -->
      <el-table :data="roleList" border style="width: 100%" stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item1, index1) in scope.row.children"
              :key="item1.id"
              :class="['dbbuttom', index1 == 0 ? 'dbtop' : '']"
            >
              <!-- 渲染一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="delClose(scope.row, item1.id)">{{
                  item1.authName
                }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 渲染二级权限 -->
                <el-row
                  v-for="(item2, index2) in item1.children"
                  :key="item2.id"
                  :class="['dbtop', index2 == 0 ? 'dbbuttom' : '']"
                >
                  <el-col :span="6">
                    <el-tag
                      closable
                      type="success"
                      @close="delClose(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级权限 -->
                  <el-col :span="18">
                    <el-tag
                      closable
                      type="warning"
                      v-for="(item3, index3) in item2.children"
                      :key="item3.id"
                      @close="delClose(scope.row, item3.id)"
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <el-button size="mini" type="primary" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" icon="el-icon-delete"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-setting"
            @click="distributionRole"
            >分配权限</el-button
          >
        </el-table-column>
      </el-table>
      <!-- 树 -->
      <el-dialog
        title="分配权限"
        :visible.sync="distributionVisible"
        width="50%"
        @close="setRightDialogClose"
      >
        <el-tree
          :data="rightsList"
          :props="treePro"
          show-checkbox
          node-key="id"
          default-expand-all
          :default-checked-keys="defKeys"
          ref="treeRef"
        ></el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="distributionVisible = false">取 消</el-button>
          <el-button type="primary" @click="distributionRights"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      roleList: [],
      addDialog: false,
      distributionVisible: false,
      treePro: {
        label: "authName",
        children: "children",
      },
      rightsList: [],
      defKeys: [],
    };
  },
  created() {
    this.rolesList();
  },
  methods: {
    // 数据
    async rolesList() {
      const { data: roleRes } = await this.axios.get(
        "https://www.liulongbin.top:8888/api/private/v1/roles"
      );
      // console.log(roleRes);
      if (roleRes.meta.status !== 200) {
        return this.$message.error(roleRes.meta.msg);
      } else {
        this.roleList = roleRes.data;
        this.$message.success(roleRes.meta.msg);
      }
    },
    // 删除
    async delClose(role, row) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult != "confirm") {
        return this.$message.info("已经取消删除");
      }

      // console.log(role, row);
      const { data: delRes } = await this.axios.delete(
        `https://www.liulongbin.top:8888/api/private/v1/roles/${role.id}/rights/${row}`
      );
      // console.log(delRes);
      if (delRes.meta.status !== 200) {
        return this.$message.error(delRes.meta.msg);
      } else {
        // this.$message.success(delRes.meta.msg);
        role.children = delRes.data;
      }
    },
    // 分配权限
    async distributionRole() {
      this.distributionVisible = true;
      const { data: distributionRes } = await this.axios.get(
        "https://www.liulongbin.top:8888/api/private/v1/rights/tree"
      );
      console.log(distributionRes);
      if (distributionRes.meta.status !== 200) {
        return this.$message.error("获取权限失败");
      } else {
        this.rightsList = distributionRes.data;
      }
    },

    //监听授权框取消按钮的事件
    setRightDialogClose() {
      this.defKeys = [];
    },
    //分配权限的授权操作
    distributionRights() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];
      const idKey = keys.join(",");
      this.axios
        .post(
          `https://www.liulongbin.top:8888/api/private/v1/roles/${this.roleId}/`,
          { rids: idKey }
        )
        .then((res) => {
          if (res.data.meta.status !== 200) {
            this.$message.error("分配权限失败");
          }
          // console.log();
          // this.$message.success("分配权限成功");

          this.rolesList();
        });
      this.distributionVisible = false;
    },
  },
};
</script>
<style lang='scss' scoped>
.el-tag {
  margin: 7px;
}

.dbtop {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #eee;
  border-top: 0px;
  border-bottom: 0px;
  margin: 1px;
}
.dbbuttom {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid #eee;
}
</style>